package com.mdd.admin.config.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mchange.v1.util.CollectionUtils;
import com.mdd.admin.service.impl.OrderManageServiceImpl;
import com.mdd.common.entity.delivery.ExpressCompany;
import com.mdd.common.entity.order.Order;
import com.mdd.common.enums.OrderEnum;
import com.mdd.common.exception.OperateException;
import com.mdd.common.mapper.delivery.ExpressCompanyMapper;
import com.mdd.common.mapper.order.OrderMapper;
import com.mdd.common.plugin.delivery.DeliveryDriver;
import com.mdd.common.plugin.delivery.vo.KdQueryTrackParam;
import com.mdd.common.plugin.delivery.vo.KdTrackResultVo;
import com.mdd.common.util.ConfigUtils;
import com.mdd.common.util.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class AutomaticReceivingTask {
    @Resource
    OrderMapper orderMapper;

    @Resource
    ExpressCompanyMapper expressCompanyMapper;

    @Resource
    OrderManageServiceImpl orderManageService;


    @Scheduled(cron = "0 0/5 * * * ?")
    public void autoRece() throws ParseException {
        System.out.println("join task=====");
        List<Order> orderList = new ArrayList<Order>();
        //获取所有待收货订单
        orderList = orderMapper.selectList(new QueryWrapper<Order>()
                .eq("pay_is", 1)
                .eq("order_status", OrderEnum.ORDER_STATUS_TAKE_DELIVER.getCode())
                .eq("is_delete", 0)
                .eq("delivery_type", 1));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("join task===== ++ " + orderList.size());
        if(orderList.size() > 0){
            for(Order order:orderList){
                System.out.println("物流订单id:" + order.getId());
                try {
                    String expressEngine = ConfigUtils.get("logistics", "engine", "");
                    if (StringUtils.isEmpty(expressEngine)) {
                        throw new OperateException("请联系管理员配置物流设置");
                    }
                    // 物流公司
                    ExpressCompany expressCompany = expressCompanyMapper.selectById(order.getExpressId());
                    // 快递查询参数
                    KdQueryTrackParam kdQueryTrackParam = new KdQueryTrackParam();
                    // 物流公司编码
                    String expressCode = expressCompany.getCodeKdniao();
                    if(expressEngine.equals("kd100")) {
                        expressCode = expressCompany.getCodeKd100();
                        if (expressCode.equals("SF") || expressCode.equals("sf") || expressCode.equals("shunfeng")) {
                            kdQueryTrackParam.setPhone(order.getAddressMobile());
                        }
                    } else {
                        if (expressCode.equals("SF")) {
                            kdQueryTrackParam.setPhone(order.getAddressMobile().substring(order.getAddressMobile().length() - 4));
                        }
                    }
                    kdQueryTrackParam.setCom(expressCode);
                    kdQueryTrackParam.setNum(order.getExpressNo());
                    // 物流信息
                    List<KdTrackResultVo> track = (new DeliveryDriver()).queryTrack(kdQueryTrackParam);
                    for(KdTrackResultVo kdTrackResultVo:track){
                        //如果物流确认收货了 48小时后自动收货
                        if(kdTrackResultVo.getContent().contains("代收") || kdTrackResultVo.getContent().contains("已签收")){
                            System.out.println(kdTrackResultVo.getContent() + "====time-====" +kdTrackResultVo.getTime());
                            Date date = dateFormat.parse(kdTrackResultVo.getTime());
                            long timestamp = date.getTime() / 1000;
                            //暂定五分钟自动收货
                            Long receTime = timestamp + 60 * 60 * 48;
                            if(receTime <= System.currentTimeMillis() / 1000){
                                orderManageService.confirm(order.getId());
                            }
                            //OrderManageServiceImpl orderManageService = new OrderManageServiceImpl();
                        }
                    }
                } catch (OperateException e) {
                    // 查询中错误信息
                    System.out.println("物流记录为空");
                    //vo.setTrackError(e.getMsg());
                }
            }
        }

    }

}
